<?php

declare(strict_types=1);

/**
 * Author: Jesse
 * Email : jessedev@163.com
 * Date: 2024/6/16 23:25
 * FileName: Facade.php
 * DESC: The Relentless Pursuit of Perfection.
 */

namespace Common\Kernel\Facades;


use function Hyperf\Support\make;

abstract class Facade
{

    protected static array $instance = [];

    /**
     * @param $method
     * @param $arg
     * @return mixed
     */
    public static function __callStatic($method, $arg)
    {
        $instance = static::getInstance(static::getFacadeAccessor());
        return call_user_func_array([$instance, $method], $arg);
    }

    /**
     * 获取实例
     *
     * @param $classname
     * @return mixed
     */
    protected static function getInstance($classname): mixed
    {
        if (isset(self::$instance[$classname]) && self::$instance[$classname] instanceof $classname) {
            return self::$instance[$classname];
        }

        self::$instance[$classname] = make($classname);

        return self::$instance[$classname];
    }

    /**
     * 获取实例
     *
     * @return string
     */
    abstract protected static function getFacadeAccessor() :string;
}
